﻿using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;

[StructLayout(LayoutKind.Sequential)]
public struct WTS_SESSION_INFO
{
    public Int32 SessionID;

    [MarshalAs(UnmanagedType.LPStr)]
    public String pWinStationName;

    public ConnectionState State;
}

[StructLayout(LayoutKind.Sequential)]
public struct WTS_CLIENT_ADDRESS
{
    public uint AddressFamily;
    [MarshalAs(UnmanagedType.ByValArray,SizeConst=20)]
    public byte[] Address;
}

//WTS_CONNECTSTATE_CLASS
public enum ConnectionState
{
    Active,
    Connected,
    ConnectQuery,
    Shadow,
    Disconnected,
    Idle,
    Listen,
    Reset,
    Down,
    Init
}

public enum InfoClass
{
    InitialProgram,
    ApplicationName,
    WorkingDirectory,
    OEMId,
    SessionId,
    UserName,
    WinStationName,
    DomainName,
    ConnectState,
    ClientBuildNumber,
    ClientName,
    ClientDirectory,
    ClientProductId,
    ClientHardwareId,
    ClientAddress,
    ClientDisplay,
    ClientProtocolType
}



/// <summary>
/// Summary description for TerminalSessionsNative
/// </summary>
static public class TerminalSessionsNative
{
    static public IntPtr CurrentServer
    {
        get { return IntPtr.Zero; }
    }
    [DllImport("wtsapi32.dll")]
    public static extern int WTSEnumerateSessions(
                IntPtr hServer,
                [MarshalAs(UnmanagedType.U4)] int Reserved,
                [MarshalAs(UnmanagedType.U4)] int Version,
                ref IntPtr ppSessionInfo,
                [MarshalAs(UnmanagedType.U4)] ref int pCount);

    [DllImport("wtsapi32.dll")]
    public static extern void WTSFreeMemory(IntPtr pMemory);

    [DllImport("Wtsapi32.dll",CharSet=CharSet.Ansi)]
    public static extern bool WTSQuerySessionInformation(
        IntPtr hServer, int sessionId, InfoClass wtsInfoClass, out IntPtr ppBuffer, out uint pBytesReturned);
}
